package com.sms.controller;

import com.sms.pojo.*;
import com.sms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    ClassStudyService classStudyService;
    @Autowired
    ClassInfoService classInfoService;
    @Autowired
    CourseResultService courseResultService;
    @Autowired
    LoginInfoService loginInfoService;
    @Autowired
    StudentInfoService studentInfoService;
    //进入登录页面
    @RequestMapping("/")
    public String index(HttpSession session) {
        session.removeAttribute("student");
        session.removeAttribute("admin");
        session.removeAttribute("msg");
        return "index";
    }
    //判断登录的是学生还是管理员
    @RequestMapping("/login")
    public String login(String username, String password, String soa, HttpSession session, Model model) {
        if("student".equals(soa)){
//            return "student_indexUI";
            Integer sid;
            try{
                sid = Integer.valueOf(username);
            }catch (Exception e){
                return "redirect:/";
            }
            StudentInfo studentInfo = studentInfoService.LoginStudent(sid, password);
            if(studentInfo!=null){
                List<CourseResult> courseResults = courseResultService.selectCourseResultBySid(Integer.valueOf(username));
                session.setAttribute("student",studentInfo);
                session.setAttribute("soa",soa);
                model.addAttribute("courseResults",courseResults);
                return "student_indexUI";
            }else{
                return "redirect:/";
            }
        }else if ("admin".equals(soa)){
            LoginInfo loginInfo = loginInfoService.selectInf(new LoginInfo(null,username,password));
            if(loginInfo!=null){
                List<ClassStudy> classStudies = classStudyService.selectClassStudyAll();
                model.addAttribute("classStudies",classStudies);
                session.setAttribute("admin",loginInfo);
                session.setAttribute("soa",soa);
                return "class_information";
            }
            else {
                return "redirect:/";
            }
        }else {
            return "redirect:/";
        }
    }
}
